<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SrDetail;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SrDetailController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($project_id)
    {
        return Grid::make(new SrDetail('project'), function (Grid $grid) use ($project_id) {
            $grid->disableEditButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableViewButton();

            $grid->model()->where('project_id', $project_id);
            $grid->column('name');
            $grid->column('method')->using([
                'get' => 'get',
                'post' => 'post',
                'put' => 'put',
                'delete' => 'delete'
            ])->badge();
            $grid->column('on')->using([
                0 => '开启',
                1 => '关闭'
            ])->badge([
                'default' => 'primary',
                0 => 'success',
                1 => 'danger',
            ]);
            $grid->column('url')->badge(Admin::color()->info());
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->column('copy','接口地址')->display(function(){

                $url = "'".url('/').$this->project->baseurl.$this->url."'";
                return '<button id="copyurl" class="btn btn-primary btn-outline" onclick="copyUrl('.$url.')">复制api接口地址</button>';
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');

            });
        });
    }

    /**
     * 接口列表
     * @param Content $content
     * @return Content
     */
    public function detail_index($project_id, Content $content)
    {

        $project = \App\Models\SrProject::find($project_id);
        return $content
            ->title($project->name.'接口')
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body('<div style="margin-bottom: 20px;font-size: 16px">项目名称：'.$project->name.'</div>')
            ->body($this->grid($project_id))
            ->body('<input id="hide" type="text" style="opacity: 0"/>')
            ->body('<script>
function copyUrl(url){
    $("#hide").val(url);
    $("#hide").select();
    try {
        var state = document.execCommand("copy");
    }catch(err){
        var state = false;
    }
    if(state){
        Dcat.success("复制成功！");
    }else{
        Dcat.error("复制失败！");
    }
};
</script>');
    }

    public function detail_create($project_id, Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['create'] ?? trans('admin.create'))
            ->body($this->form($project_id));
    }

    public function detail_edit($project_id, $id, Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['edit'] ?? trans('admin.edit'))
            ->body($this->form($project_id)->edit($id));
    }

    public function detail_update($project_id, $id)
    {
        return $this->form($project_id)->update($id);
    }

    public function detail_store($project_id)
    {
        return $this->form($project_id)->store();
    }

    public function detail_destroy($project_id, $id)
    {
        return $this->form($project_id)->destroy($id);
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($project_id)
    {
        return Form::make(new SrDetail(), function (Form $form) use ($project_id) {
            $form->text('name')->required();
            $form->radio('method')->options([
                'get' => 'get',
                'post' => 'post',
                'put' => 'put',
                'delete' => 'delete'
            ])->required()->default('get');
            $form->radio('on')->options([
                0 => '开启',
                1 => '关闭'
            ])->required()->default(0);
            $form->text('url')->required()->help('示例：/shop');
//            $form->textarea('description');
            $form->textarea('result')->required()->help('请填写规范的json格式数据');
            $form->hidden('project_id');
            $form->saving(function (Form $form) use ($project_id) {
                // 修改用户提交的数据
                $form->project_id = $project_id;
            });
        });
    }
}
